package ar.com.ford.it.fullEconomics.logic;

import java.sql.Timestamp;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dao.AgreementDao;
import ar.com.ford.it.fullEconomics.persistence.dao.AgreementItemDao;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr03Agreement;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr04Agreemitem;
import ar.com.ford.it.fullEconomics.struct.AgreementFindStruct;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;

public class AgreementLogic {
    private static final String CLASS_NAME = CommodityLogic.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * crea el agreement pasado por parametro
     * 
     * @param req
     * @param user
     * @return
     * @throws FullEconomicsException
     */
    public Teesr03Agreement createAgreement(Teesr03Agreement agreement,
                                            FullEconomicsWslxUser user)
            throws FullEconomicsException {

        String METHOD_NAME = "createAgreement";
        log.entering(CLASS_NAME, METHOD_NAME);

        AgreementDao dao = new AgreementDao();

        try {
            PersistenceUtil.newTransaction();
            agreement
                    .setEesr03CreateS(new Timestamp(System.currentTimeMillis()));
            agreement.setEesr03CreateUserC(user.getId());
            agreement.setEesr03LastUpdtS(new Timestamp(System
                    .currentTimeMillis()));
            agreement.setEesr03LastUpdtUserC(user.getId());
            agreement.setEesr03PlantCodeC(user.getTeesg01Plant());
            dao.createAgreement(agreement);
            PersistenceUtil.commitTransaction();

        } catch (PersistenceException pe) {

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "PersistenceException", pe);
            try {
                PersistenceUtil.rollbackTransaction();
            } catch (PersistenceException rollbackEx) {
                rollbackEx.printStackTrace();
            }

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Create Agreement Exception", pe);
            throw new FullEconomicsException(pe,
                    FullEconomicsPrs.MessageErrors.Agreement.CREATE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return agreement;
    }

    /**
     * guarda los cambios hechos en el agreement pasado por parametro
     * 
     * @param req
     * @param user
     * @throws FullEconomicsException
     */
    public void updateAgreement(Teesr03Agreement agreement,
                                FullEconomicsWslxUser user)
            throws FullEconomicsException {

        String METHOD_NAME = "updateAgreement";
        log.entering(CLASS_NAME, METHOD_NAME);

        AgreementDao dao = new AgreementDao();
        Teesr03Agreement workingCopy = null;

        try {
            PersistenceUtil.newTransaction();
            PersistenceUtil.getPersistenceManager().clearCache();
            workingCopy = dao.getAgreementForUpdate(agreement
                    .getTeess01Supplier(), agreement
                    .getEesr03AgreeEcolevelDateY(), user.getTeesg01Plant());

            workingCopy.setEesr03AgreeEffectiveDateY(agreement
                    .getEesr03AgreeEffectiveDateY());
            workingCopy.setEesr03AgreeIssueDateY(agreement
                    .getEesr03AgreeIssueDateY());
            workingCopy.setEesr03AgreeSignatureDateY(agreement
                    .getEesr03AgreeSignatureDateY());
            workingCopy.setEesr03AgreeDescriptionX(agreement
                    .getEesr03AgreeDescriptionX());
            workingCopy.setEesr03AgreeCommentsX(agreement
                    .getEesr03AgreeCommentsX());
            PersistenceUtil.commitTransaction();

        } catch (PersistenceException pe) {

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "PersistenceException", pe);
            try {
                PersistenceUtil.rollbackTransaction();
            } catch (PersistenceException rollbackEx) {
                rollbackEx.printStackTrace();
            }

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Update Agreement item Exception", pe);
            throw new FullEconomicsException(pe,
                    FullEconomicsPrs.MessageErrors.Agreement.UPDATE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * elimina el agreement pasado por parametro
     * 
     * @param req
     * @throws FullEconomicsException
     */
    public void deleteAgreement(Teesr03Agreement agreement)
            throws FullEconomicsException {

        String METHOD_NAME = "deleteAgreement";
        log.entering(CLASS_NAME, METHOD_NAME);

        AgreementDao dao = new AgreementDao();
        AgreementItemDao itemDao = new AgreementItemDao();
        AgreementItemLogic itemLogic = new AgreementItemLogic();
        Collection<Teesr04Agreemitem> items = itemLogic
                .findAgreementItems(agreement);
        try {
            PersistenceUtil.newTransaction();
            for(Teesr04Agreemitem item : items)
                itemDao.deleteAgreementItem(item);
            dao.deleteAgreement(agreement);
            PersistenceUtil.commitTransaction();

        } catch (PersistenceException pe) {

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "PersistenceException", pe);
            try {
                PersistenceUtil.rollbackTransaction();
            } catch (PersistenceException rollbackEx) {
                rollbackEx.printStackTrace();
            }

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "delete Agreement item Exception", pe);
            throw new FullEconomicsException(pe,
                    FullEconomicsPrs.MessageErrors.Agreement.DELETE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * busca todos los agreement que cumplan con con lo especificado por el
     * filtro
     * 
     * @param findStruct
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesr03Agreement> findAgreements(
                                                       AgreementFindStruct findStruct)
            throws FullEconomicsException {

        String METHOD_NAME = "findAgreements";
        log.entering(CLASS_NAME, METHOD_NAME);

        AgreementDao dao = new AgreementDao();
        Collection<Teesr03Agreement> ret;
        try {
            ret = dao.findAgreements(findStruct);
        } catch (PersistenceException pe) {

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "PersistenceException", pe);
            try {
                PersistenceUtil.rollbackTransaction();
            } catch (PersistenceException rollbackEx) {
                rollbackEx.printStackTrace();
            }

            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Find Agreements Exception", pe);
            throw new FullEconomicsException(pe,
                    FullEconomicsPrs.MessageErrors.Agreement.FIND);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return ret;
    }
}
